package com.fita.admin.web.controller.order;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.order.EnquiryQuery;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.order.EnquiryService;
import com.fita.admin.web.resolver.Secure;
import com.fita.entity.order.reverse.Enquiry;
import com.fita.entity.order.reverse.ReverseOrderType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @author: 11200
 * @date: 2019/8/23
 */
@Controller
@RequestMapping("/enquiries")
@MenuSetting("menu-order")
public class EnquiryController {

    private static final Logger logger = LoggerFactory.getLogger(EnquiryController.class);

    @Autowired
    private EnquiryService enquiryService;

    /**
     *
     * @return
     * @param request
     */
    @GetMapping
    @MenuSetting("submenu-{type.toString().toLowerCase()}-reverse-order-list")
    public ModelAndView list(HttpServletRequest request,
                             ReverseOrderType type,
                             Page page,
                             EnquiryQuery query){

        ModelAndView modelAndView = new ModelAndView("order/reverse/order-list");

        int count = enquiryService.count(query);

        if (count > 0){

            List<Enquiry> enquiries = enquiryService.search(Enquiry.class, query, page.with(count));

            modelAndView.addObject("enquiries", enquiries);
        }
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter", "page"));

        modelAndView.addObject("type", type);

        modelAndView.addObject("isEnquiry", true);

        modelAndView.addObject("page", page);

        return modelAndView;
    }


    /**
     *
     * @param enquiryId
     * @param login
     * @return
     */
    @GetMapping(path = "/{enquiryId}/create-reverse-order")
    public ModelAndView createOrder(@PathVariable Long enquiryId,
                                    @Secure Login login){

        ModelAndView modelAndView = new ModelAndView("order/reverse/reverse-order-create");

        Enquiry enquiry = enquiryService.load(Enquiry.class, enquiryId);

        modelAndView.addObject("type", enquiry.getType());

        modelAndView.addObject("enquiry", enquiry);

        return modelAndView;
    }

    /**
     *
     * @param enquiryId
     * @param login
     * @return
     */
    @PostMapping(path = "/{enquiryId}/create-reverse-order", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public HttpResult onCreateOrder(@PathVariable Long enquiryId,
                                    @Secure Login login){
        try {
            enquiryService.createReverseOrder(enquiryId, login.getId());

            return new HttpResult().success();

        } catch (Exception e) {

            logger.error("#onCreateOrder() [{}]", e);

            return new HttpResult().fail("500").message(e.getMessage());
        }
    }

}
